package pv1682010.gui;

import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;
import javax.sql.DataSource;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;
import pv1682010.Course;
import pv1682010.CourseManager;
import pv1682010.Database;
import pv1682010.Event;

/**
 *
 * @author Petr
 */
public class EventTableModel extends AbstractTableModel {

    private CourseManager cm;
    private Course course = null;
    private ResourceBundle translations;
    private List<Event> events;

    public EventTableModel() {
        translations = Translations.getRb();
        DataSource ds = new Database();
        cm = new CourseManager(ds);
    }

    public CourseManager getCourseManager() {
        return cm;
    }

    @Override
    public int getRowCount() {
        int rowCount = 0;
        if (course != null) {
            rowCount = events.size();
        }
        return rowCount;
    }

    @Override
    public int getColumnCount() {
        return 4;
    }

    @Override
    public String getColumnName(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return translations.getString("event_number");
            case 1:
                return translations.getString("event_name");
            case 2:
                return translations.getString("event_start");
            case 3:
                return translations.getString("event_end");
            default:
                throw new IllegalArgumentException("columnIndex");
        }
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        DateFormat locDate = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, Translations.getLocale());
        Event event = events.get(rowIndex);
        switch (columnIndex) {
            case 0:
                return event.getEventNumber();
            case 1:
                return event.getEventName();
            case 2:
                return locDate.format(event.getStartDate());
            case 3:
                return locDate.format(event.getEndDate());
            default:
                throw new IllegalArgumentException("columnIndex");
        }
    }

    public void setCourse(int courseNumber) {
        try {
            this.course = cm.getCourseById(courseNumber);
            setEvents();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, translations.getString("err_e_list"));
        }
    }

    private void setEvents() {
        try {
            events = new ArrayList(cm.getEventsForCourse(course));
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, translations.getString("err_e_list"));
        }
    }

    public void addEvent(Event event) {
        try {
            event.setCourse(course);
            cm.addEvent(event);
            int lastRow = cm.getAllCourses().size() - 1;
            setEvents();
            fireTableRowsInserted(lastRow, lastRow);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, translations.getString("err_e_ins"));
        }
    }

    public void deleteEvent(Event event) {
        try {
            event.setCourse(course);
            cm.removeEvent(event);
            int lastRow = cm.getEventsForCourse(course).size() - 1;
            setEvents();
            fireTableRowsDeleted(lastRow, lastRow);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, translations.getString("err_e_del"));
        }
    }

    @Override
    public void setValueAt(Object value, int row, int col) {
        try {
            Event event = events.get(row);
            if (col == 1) {
                event.setEventName(value.toString());
            }
            if (col == 2) {
                DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm");
                Date date = df.parse(value.toString());
                event.setStartDate(date);
            }
            if (col == 3) {
                DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm");
                Date date = df.parse(value.toString());
                event.setEndDate(date);
            }
            cm.updateEvent(event);
            setEvents();
            fireTableCellUpdated(row, col);
        } catch (ParseException ex) {
            JOptionPane.showMessageDialog(null, translations.getString("date_format"));
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, translations.getString("err_e_upd"));
        }
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        if (columnIndex > 0) {
            return true;
        } else {
            return false;
        }
    }
}